package net.toocruel.iqismart.web.controller.home;

import net.toocruel.iqismart.dao.condition.ArticlesCondition;
import net.toocruel.iqismart.entity.common.Category;
import net.toocruel.iqismart.entity.constants.CategoryVisibility;
import net.toocruel.iqismart.entity.core.Article;
import net.toocruel.iqismart.entity.setting.Setting;
import net.toocruel.iqismart.service.UserService;
import net.toocruel.iqismart.service.core.ArticleService;
import net.toocruel.iqismart.service.core.CategoryService;
import net.toocruel.iqismart.service.core.QuestionService;
import net.toocruel.iqismart.service.core.TagService;
import net.toocruel.iqismart.web.controller.base.AbstractBaseController;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description
 * @Auther sty
 * @createTime 2018/5/14 下午2:12
 */
@Controller
@RequestMapping("articles")
public class HomeArticlesController extends AbstractBaseController {
    @Autowired
    private QuestionService questionService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private UserService userService;

    @Autowired
    private TagService tagService;

    @Autowired
    private ArticleService articleService;


    @RequestMapping("")
    public ModelAndView questions(HttpServletRequest request,
                                  HttpServletResponse response,
                                  @RequestParam(defaultValue = "14") int size,
                                  @RequestParam(defaultValue = "1") int page,
                                  @RequestParam(defaultValue = "id") String sortBy,
                                  @RequestParam(defaultValue = "desc") String sortOrder,
                                  @RequestParam(defaultValue = "newest") String filter){
        //
        return processArticles(size, page, sortBy, sortOrder, null, filter);
    }

    @RequestMapping("{categorySlug}")
    public ModelAndView questionsCategory(HttpServletRequest request,
                                  HttpServletResponse response,
                                  @RequestParam(defaultValue = "14") int size,
                                  @RequestParam(defaultValue = "1") int page,
                                  @RequestParam(defaultValue = "id") String sortBy,
                                  @RequestParam(defaultValue = "desc") String sortOrder,
                                  @PathVariable String categorySlug,
                                  @RequestParam(defaultValue = "newest") String filter){

        return processArticles(size, page, sortBy, sortOrder, categorySlug, filter);
    }

    private ModelAndView processArticles(int size,int page,String sortBy,String sortOrder,String categorySlug, String filter) {
        ModelAndView modelAndView = new ModelAndView(getTemplate("articles"));
        ArticlesCondition condition = new ArticlesCondition();
        condition.setCategorySlug(categorySlug);
        condition.setFilter(filter);

        List<Category> categories = categoryService.findByVisibilityAndRoles(CategoryVisibility.articles, getCurrentUser());

        Page<Article> articles = articleService.findAll(condition,new PageRequest(page-1, size,new Sort(Sort.Direction.fromString(sortOrder),sortBy)));

        List<Article> hotArticles = articleService.getHotArticles(8);
        modelAndView.addObject("hotArticles", hotArticles);
        modelAndView.addObject("hotTags",tagService.getHotTags(20));
        modelAndView.addObject("hotUsers",articleService.getHotUsers(8));
        modelAndView.addObject("categories",categories);
        modelAndView.addObject("articles",articles);
        modelAndView.addObject("condition",condition);

        //seoTitle
        String seoTitle = "";
        if(StringUtils.isEmpty(categorySlug)){
            seoTitle += "全部 - ";
        }else{
            seoTitle += categoryService.findBySlug(categorySlug).getName()+" - ";
        }
        seoTitle += getFilterName(filter);
        seoTitle+=" - 文章 - ";
        seoTitle+=Setting.SETTING.getWebsite().getName();

        modelAndView.addObject("seoTitle",seoTitle);

        return modelAndView;
    }

    /**
     * 转换页面filter参数到字符串 以便显示
     * @param filter
     * @return
     */
    private String getFilterName(@RequestParam(defaultValue = "newest") String filter) {
        String filterName = "";
        if("newest".equals(filter)){
            filterName += "最新发表";
        }else if("newcomment".equals(filter)){
            filterName += "最新评论";
        }else if("mostview".equals(filter)){
            filterName += "最多查看";
        }else if("mostcomment".equals(filter)){
            filterName += "最多评论";
        }else if("best".equals(filter)){
            filterName += "精华";
        }else if("recommend".equals(filter)){
            filterName += "推荐";
        }
        return filterName;
    }


    @Override
    protected String getModule() {
        return "home";
    }

}
